package com.pozu.new_retail.controllers;

import com.pozu.new_retail.dao.base.JsonResult;
import com.pozu.new_retail.dao.model.CardDetail;
import com.pozu.new_retail.service.CardDetailService;
import com.pozu.new_retail.utils.CodeConstants;
import com.pozu.new_retail.utils.CodeProperties;
import com.pozu.new_retail.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * 名片 Coltroller
 *
 * @author shenxueyuan
 * @Date 2019/3/15
 */
@Controller
public class CardDetailController {

    // 名片详情的service
    @Autowired
    private CardDetailService cardDetailService;

    /**
     * 查询某个企业下的 名片列表
     *
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/api/card/list", method = RequestMethod.GET)
    public Object getCardDetailList(HttpServletRequest request) throws Exception {

        int appId = Integer.parseInt(request.getHeader("appId"));//哪个小程序

        String staffId = request.getParameter("staffId");//工作人员id

        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("appId", appId);
        if(!"".equals(staffId)){
            hashMap.put("staff_id", staffId);
        }

        JsonResult jr = new JsonResult();

        jr.respCode = CodeConstants.SUCCESS_CODE;
        jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);

        jr.respData = cardDetailService.queryByMap(hashMap);
        return jr;
    }

    /**
     * 查询某个人的名片详情
     * {PS：这个不是聚合接口，仅仅是名片的资料详情}
     *
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/api/card/detail", method = RequestMethod.GET)
    public Object queryByStaffId(HttpServletRequest request) throws Exception {

        int appId = Integer.parseInt(request.getHeader("appId"));//哪个小程序

        int staffId = Integer.parseInt(request.getParameter("staffId"));//工作人员id

        JsonResult jr = new JsonResult();

        jr.respCode = CodeConstants.SUCCESS_CODE;
        jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);

        jr.respData = cardDetailService.queryByStaffId(staffId);
        return jr;
    }

    /**
     * 名片主页 聚合信息
     * 包含内容【名片详情、浏览记录、印象标签、商品推荐、我的照片】
     *
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/api/card/profile", method = RequestMethod.GET)
    public Object getCardProfile(HttpServletRequest request) throws Exception {

        int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序
        int staffId = Integer.parseInt(request.getParameter("staffId"));//工作人员id

        JsonResult jr = new JsonResult();
        jr.respCode = CodeConstants.SUCCESS_CODE;
        jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);

        jr.respData = cardDetailService.queyCardProfile(appId, staffId);

        return jr;
    }

    /**
     * 新增一个名片
     *
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/api/card/save", method = RequestMethod.POST)
    public Object addCardDetail(HttpServletRequest request) throws Exception {

        int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序

        CardDetail cardDetail = Tools.getRequestObject(request, CardDetail.class);
        cardDetail.setAppId(appId);
        cardDetail.setCreateTime(System.currentTimeMillis());

        JsonResult jr = new JsonResult();
        jr.respCode = CodeConstants.SUCCESS_CODE;
        jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);

        cardDetailService.insertCardDetail(cardDetail);

        return jr;
    }

    /**
     * 修改一个名片
     *
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/api/card/update", method = RequestMethod.PUT)
    public Object updateActivity(HttpServletRequest request) throws Exception {

        CardDetail cardDetail = Tools.getRequestObject(request, CardDetail.class);

        JsonResult jr = new JsonResult();
        jr.respCode = CodeConstants.SUCCESS_CODE;
        jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);

        cardDetailService.updateCardDetail(cardDetail);

        return jr;
    }

}
